package cn.iocoder.yudao.module.energy.service.upload;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.energy.controller.admin.upload.vo.*;
import cn.iocoder.yudao.module.energy.dal.dataobject.upload.UploadDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.energy.dal.mysql.upload.UploadMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.energy.enums.ErrorCodeConstants.*;

/**
 * 文件上传 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UploadServiceImpl implements UploadService {

    @Resource
    private UploadMapper uploadMapper;

    @Override
    public Long createUpload(UploadSaveReqVO createReqVO) {
        // 插入
        UploadDO upload = BeanUtils.toBean(createReqVO, UploadDO.class);
        uploadMapper.insert(upload);
        // 返回
        return upload.getId();
    }

    @Override
    public void updateUpload(UploadSaveReqVO updateReqVO) {
        // 校验存在
        validateUploadExists(updateReqVO.getId());
        // 更新
        UploadDO updateObj = BeanUtils.toBean(updateReqVO, UploadDO.class);
        uploadMapper.updateById(updateObj);
    }

    @Override
    public void deleteUpload(Long id) {
        // 校验存在
        validateUploadExists(id);
        // 删除
        uploadMapper.deleteById(id);
    }

    private void validateUploadExists(Long id) {
        if (uploadMapper.selectById(id) == null) {
            throw exception(UPLOAD_NOT_EXISTS);
        }
    }

    @Override
    public UploadDO getUpload(Long id) {
        return uploadMapper.selectById(id);
    }

    @Override
    public PageResult<UploadDO> getUploadPage(UploadPageReqVO pageReqVO) {
        return uploadMapper.selectPage(pageReqVO);
    }

}